package com.king.base.rest.dict;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.king.base.aop.Log;
import com.king.base.domain.dict.DictType;
import com.king.base.exception.BadRequestException;
import com.king.base.auth.utils.EntityUtils;
import com.king.base.utils.PageUtil;
import com.king.base.service.dict.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Spur
 * @date 2019/6/24
 */
@RestController
@RequestMapping(value = "api/core/dict")
@Api(value = "dict",description = "字典管理Controller")
public class DictController {
    @Autowired
    private DictService dictService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "分页列表查询")
    @Log(description = "查询字典")
    public ResponseEntity page(DictType dictType, Pageable pageable){
        return new ResponseEntity(PageUtil.toPage(dictService.page(pageable, dictType)),HttpStatus.OK);
    }

    @PostMapping(value = "/create")
    @ApiOperation(value = "字典创建")
    @Log(description = "字典创建")
    public ResponseEntity create(@RequestBody DictType dictType){

        return new ResponseEntity(dictService.saveOrUpdate(dictType),HttpStatus.CREATED);
    }
    @PutMapping(value = "/update")
    @ApiOperation(value = "字典编辑")
    @Log(description = "字典编辑")
    public ResponseEntity update(@RequestBody DictType dictType){
        return new ResponseEntity(dictService.saveOrUpdate(dictType),HttpStatus.OK);
    }

    @DeleteMapping(value = "/delete/{dictId}/{dictCode}")
    @ApiOperation(value = "字典删除")
    @Log(description = "字典删除")
    public ResponseEntity delete(@PathVariable String dictId,@PathVariable String dictCode){
        dictService.delete(dictId,dictCode);
        return new ResponseEntity(HttpStatus.OK);
    }
    @GetMapping(value = "/getById/{dictId}")
    @ApiOperation(value = "获取单个字典")
    public ResponseEntity getById(@PathVariable String dictId){
        return new ResponseEntity(dictService.getById(dictId),HttpStatus.OK);
    }
}
